<?php

namespace App\Service\Kms\App;


use App\Http\Resources\Kms\App\VersionListResource;
use App\Models\Kms\App\ClientsModel;
use App\Models\Kms\App\ClientsVersionModel;
use App\Models\Kms\App\ClientsVersionReleaseHistoryModel;
use App\Service\Kms\KmsBaseService;
use Exception;
use Illuminate\Support\Facades\DB;

class ClientVersionService extends KmsBaseService
{

    /**
     * @param object $params
     * @return array
     */
    public static function list(object $params): array
    {
        $pageSize = attributeGet($params, "pageSize", 10);
        $page     = attributeGet($params, "pageNum", 1);
        $paginate = ClientsVersionModel::query()->where("clientId", $params->clientId)
                                       ->withCount("versionReleaseHistoryRel")
                                       ->orderByDesc("id")
                                       ->paginate($pageSize, ["*"], "pageNum", $page);
        return [
            "list"     => VersionListResource::collection($paginate->items()),
            "pageNum"  => $page,
            "pageSize" => $pageSize,
            "count"    => $paginate->total()
        ];
    }

    /**
     * @param object $params
     */
    public static function addVersion(object $params)
    {
        try {
            DB::beginTransaction();
            $params->nowTime = time();
            $versionId       = ClientsVersionModel::query()->insertGetId([
                "clientId"             => $params->clientId,
                "version"              => $params->version,
                "versionChangeContent" => $params->versionChangeContent,
                "packageUrl"           => $params->packageUrl,
                "packagingToolVersion" => $params->packagingToolVersion,
                "createdAt"            => $params->nowTime,
                "updatedAt"            => $params->nowTime,
                "createdByUid"         => request()->user()->id,
                "updatedByUid"         => request()->user()->id,
            ]);

            if ($params->releaseVersion) {
                ClientsModel::query()->where("id", $params->clientId)->update([
                    "versionId"    => $versionId,
                    "updatedAt"    => $params->nowTime,
                    "createdByUid" => request()->user()->id,
                    "updatedByUid" => request()->user()->id,
                ]);

                ClientsVersionReleaseHistoryModel::query()->insert([
                    "versionId"            => $versionId,
                    "clientId"             => $params->clientId,
                    "version"              => $params->version,
                    "versionChangeContent" => $params->versionChangeContent,
                    "packageUrl"           => $params->packageUrl,
                    "packagingToolVersion" => $params->packagingToolVersion,
                    "createdAt"            => $params->nowTime,
                    "createdByUid"         => request()->user()->id
                ]);
            }
            DB::commit();
        } catch (Exception $exception) {
            DB::rollBack();
            handleKfException($exception);
        }

    }

    /**
     * @param object $params
     */
    public static function updateVersion(object $params)
    {
        try {
            DB::beginTransaction();
            $params->nowTime = time();
            $versionId       = ClientsVersionModel::query()->where("id", $params->versionId)->update([
                "version"              => $params->version,
                "versionChangeContent" => $params->versionChangeContent,
                "packageUrl"           => $params->packageUrl,
                "packagingToolVersion" => $params->packagingToolVersion,
                "updatedAt"            => $params->nowTime,
                "updatedByUid"         => request()->user()->id,
            ]);

            if ($params->releaseVersion) {
                ClientsModel::query()->where("id", $params->clientId)->update([
                    "versionId" => $versionId,
                    "updatedAt" => $params->nowTime
                ]);

                ClientsVersionReleaseHistoryModel::query()->insert([
                    "versionId"            => $versionId,
                    "clientId"             => $params->clientId,
                    "version"              => $params->version,
                    "versionChangeContent" => $params->versionChangeContent,
                    "packageUrl"           => $params->packageUrl,
                    "packagingToolVersion" => $params->packagingToolVersion,
                    "createdAt"            => $params->nowTime,
                    "createdByUid"         => request()->user()->id,
                ]);
            }
            DB::commit();
        } catch (Exception $exception) {
            DB::rollBack();
            handleKfException($exception);
        }

    }

    /**
     * @param object $params
     */
    public static function deleteVersion(object $params)
    {
        ClientsVersionModel::query()->where("id", $params->versionId)->update([
            "isDeleted"    => 1,
            "updatedAt"    => time(),
            "updatedByUid" => request()->user()->id,
        ]);
    }

    /**
     * @param object $params
     */
    public static function releaseVersion(object $params)
    {
        try {
            DB::beginTransaction();
            $params->nowTime = time();
            $versionDetail   = ClientsVersionModel::query()->where("id", $params->versionId)->first();
            ClientsModel::query()->where("id", $params->clientId)->update([
                "versionId" => $params->versionId,
                "updatedAt" => $params->nowTime
            ]);

            ClientsVersionReleaseHistoryModel::query()->insert([
                "versionId"            => $params->versionId,
                "clientId"             => $params->clientId,
                "version"              => $versionDetail->version,
                "versionChangeContent" => $versionDetail->versionChangeContent,
                "packageUrl"           => $versionDetail->packageUrl,
                "packagingToolVersion" => $versionDetail->packagingToolVersion,
                "createdAt"            => $params->nowTime,
                "createdByUid"         => request()->user()->id,
            ]);
            DB::commit();
        } catch (Exception $exception) {
            DB::rollBack();
            handleKfException($exception);
        }
    }
}
